/**
 * 
 */
package com.alstudio.ui.module.main.fragment.rank;

import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.web.ALWebViewManager;
import com.alstudio.view.myrankinfo.MyRankInfoView;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.loovee.imaohu.R;

/**
 * 
 * 我要入帮
 * 
 * @author alonso lee
 * 
 */
public class MyRankInfoActivity extends TitleBarActivity implements ALTableViewClickListener{

	private LinearLayout tableViewForMyRankInfo;
	private ALTableView tableViewForRankInfo;

	private MyRankInfoView sexyRank;
	private MyRankInfoView glamourRank;
	private MyRankInfoView richRank;

	private ImageView my_icon;

	private final String RANK_DEFAULT_VALUE = "0";
	private String popular = RANK_DEFAULT_VALUE;
	private String rich = RANK_DEFAULT_VALUE;
	private String glamour = RANK_DEFAULT_VALUE;
	
	private String popularValue = RANK_DEFAULT_VALUE;
	private String richValue = RANK_DEFAULT_VALUE;
	private String glamourValue = RANK_DEFAULT_VALUE;
	
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.my_rank_info_layout);
		tableViewForMyRankInfo = (LinearLayout) findViewById(R.id.aLTableView1);
		tableViewForRankInfo = (ALTableView) findViewById(R.id.aLTableView2);
		my_icon = (ImageView) findViewById(R.id.icon);
		initData();
		setCenterTextViewMessage(R.string.TxtAboutRankTitle);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		initMyRankInfo();
		initRankDesc();
		loadIcon();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		requeqtData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#requeqtData()
	 */
	@Override
	public void requeqtData() {
		// TODO Auto-generated method stub
		if (isDataReceived)
			return;
		super.requeqtData();
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppGetRankingRankEvent();
			showLoadingProgress();
		}
	}

	/**
	 * 初始化我的排行榜信息
	 */
	private void initMyRankInfo() {
		//女神榜
		sexyRank = new MyRankInfoView(this);
		sexyRank.setTitle(getString(R.string.TxtSexyRankNumTitle));
		sexyRank.setSubtitle(String.format(getString(R.string.TxtRankPoint),
				popular));
		sexyRank.setDrawable(R.drawable.hot_personal_icon_01);
		sexyRank.setItemCount(String.format(getString(R.string.TxtRankNum), popular));
		sexyRank.commit();

		tableViewForMyRankInfo.addView(sexyRank);

		ImageView divider = new ImageView(this);
		divider.setImageResource(R.drawable.hot_personal_bg_line);
		tableViewForMyRankInfo.addView(divider);

		//富豪榜排名
		richRank = new MyRankInfoView(this);
		richRank.setTitle(getString(R.string.TxtSexyRankNumTitle));
		richRank.setSubtitle(String.format(getString(R.string.TxtRankMoney),
				popular));
		richRank.setDrawable(R.drawable.hot_personal_icon_03);
		richRank.setItemCount(String.format(getString(R.string.TxtRankNum), popular));
		richRank.commit();
		tableViewForMyRankInfo.addView(richRank);


		divider = new ImageView(this);
		divider.setImageResource(R.drawable.hot_personal_bg_line);
		tableViewForMyRankInfo.addView(divider);
		
		//魅力榜
		glamourRank = new MyRankInfoView(this);
		glamourRank.setTitle(getString(R.string.TxtSexyRankNumTitle));
		glamourRank.setSubtitle(String.format(getString(R.string.TxtRankGlamour),
				popular));
		glamourRank.setDrawable(R.drawable.hot_personal_icon_02);
		glamourRank.setItemCount(String.format(getString(R.string.TxtRankNum),
				popular));
		glamourRank.commit();

		tableViewForMyRankInfo.addView(glamourRank);


	}

	private void initRankDesc() {
		tableViewForRankInfo.addBasicItem(R.drawable.hot_personal_icon_04,
				getString(R.string.TxtAboutSexyRank), null);
		tableViewForRankInfo.addBasicItem(R.drawable.hot_personal_icon_04,
				getString(R.string.TxtAboutRichRank), null);
		tableViewForRankInfo.addBasicItem(R.drawable.hot_personal_icon_04,
				getString(R.string.TxtAboutGlamourRank), null);
		tableViewForRankInfo.commit();
		tableViewForRankInfo.setClickListener(this);
	}

	private void loadIcon() {
		
		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(
				ALLocalEnv.getLocalEnv().getMyProfile().getThumbAvatar(),
				my_icon,
				ALLocalEnv.getLocalEnv().getCycleImageDisplayOptions());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleRankingRankResult(com.alstudio
	 * .pojo.ALXmppEvent)
	 */
	@Override
	public void handleRankingRankResult(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleRankingRankResult(data);
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			@SuppressWarnings("unchecked")
			HashMap<String, String> ranks = (HashMap<String, String>) data
					.getData();
			popular = ranks.get("popular");
			rich = ranks.get("rich");
			glamour = ranks.get("glamour");
			
			popularValue = ranks.get("popularValue");
			richValue = ranks.get("richValue");
			glamourValue = ranks.get("glamourValue");
			
			
			//女神排行
			sexyRank.setSubtitle(String.format(getString(R.string.TxtRankPoint),
					popular));
			sexyRank.setItemCount(String.format(getString(R.string.TxtRankNum), popular));
			sexyRank.commit();
			
			//富豪排行
			richRank.setSubtitle(String.format(getString(R.string.TxtRankMoney),
					rich));
			richRank.setItemCount(String.format(getString(R.string.TxtRankNum), rich));
			richRank.commit();
			
			//魅力排行
			glamourRank.setSubtitle(String.format(
					getString(R.string.TxtRankGlamour), glamour));
			glamourRank.setItemCount(String.format(
					getString(R.string.TxtRankNum), glamour));
			glamourRank.commit();
		}
	}

	/* (non-Javadoc)
	 * @see com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick(int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		switch(index){
		case 0:
			ALWebViewManager.startWebActivity("http://www.cnbeta.com");
			break;
		case 1:
			ALWebViewManager.startWebActivity("http://www.cnbeta.com");
			break;
		case 2:
			ALWebViewManager.startWebActivity("http://www.cnbeta.com");
			break;
		}
	}

}
